﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Supply_Fetch
{
    public partial class ColumnLightSelector : UserControl
    {
        private Button[] LEDButtons;
        private Button turnAllOn;
        private Button turnAllOff;
        private int MaxCols;
        public event EventHandler columnButton_onClick;
        private Bitmap onImage;
        private Bitmap offImage;

        public ColumnLightSelector()
        {
            InitializeComponent();
        }

        public void BuildColumnSelector( int numCols )
        {
            this.LEDButtons = new Button[numCols];
            int smallHeight = ( 2 * this.Height ) / 3;
            int largeHeight = this.Height / 3;
            int smallWidth = this.Width / numCols;
            int largeWidth = (smallWidth * numCols) / 2;
            this.MaxCols = numCols;

            try
            {
                this.onImage = new Bitmap(this.LEDButtonsBGImages.Images[0], new Size(smallWidth, smallHeight));
                this.offImage = new Bitmap(this.LEDButtonsBGImages.Images[1], new Size(smallWidth, smallHeight));
            }
            catch(Exception e)
            {
               
            }

            for (int i = 0; i < this.MaxCols; i++)
            {
                this.LEDButtons[i] = new Button();
                this.LEDButtons[i].FlatStyle = FlatStyle.Flat;
                this.LEDButtons[i].FlatAppearance.BorderColor = Color.Black;
                //this.LEDButtons[i].TextAlign = ContentAlignment.TopCenter;
                this.LEDButtons[i].Height = smallHeight;
                this.LEDButtons[i].Width = smallWidth;
                this.LEDButtons[i].Name = (i+1).ToString();
                this.LEDButtons[i].Text = (i+1).ToString();
                this.LEDButtons[i].Location = new Point(i * smallWidth, 0);
                this.LEDButtons[i].Font = new Font("Microsoft Sans Serif",10.75F, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.LEDButtons[i].ForeColor = Color.FromArgb(192, 0, 0);
                if (onImage != null) ;
                    this.LEDButtons[i].BackgroundImage = onImage;

                this.LEDButtons[i].Click += new System.EventHandler(this.LEDButtons_onClick);
                this.Controls.Add(this.LEDButtons[i]);
            }

            this.turnAllOn = new Button();
            this.turnAllOn.FlatStyle=FlatStyle.Flat;
            this.turnAllOn.UseVisualStyleBackColor = false;
            this.turnAllOn.FlatAppearance.BorderSize = 0;
            this.turnAllOn.FlatAppearance.MouseOverBackColor = Color.FromArgb(192, 0, 0);
            this.turnAllOn.BackColor = Color.DodgerBlue;
            this.turnAllOn.ForeColor = Color.White;
            this.turnAllOn.Height = largeHeight;
            this.turnAllOn.Width = largeWidth;
            this.turnAllOn.Name = "turnAllOn";
            this.turnAllOn.Text = "Turn All Lights On";
            this.turnAllOn.Location = new Point(0, smallHeight);
            this.turnAllOn.Font = new Font("Microsoft Sans Serif", 15.75F, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.turnAllOn.Click += new System.EventHandler(this.TurnAllOn_onClick);
            this.Controls.Add(this.turnAllOn);

            this.turnAllOff = new Button();
            this.turnAllOff.FlatStyle = FlatStyle.Flat;
            this.turnAllOff.UseVisualStyleBackColor = false;
            this.turnAllOff.FlatAppearance.BorderSize = 0;
            this.turnAllOff.FlatAppearance.MouseOverBackColor = Color.FromArgb(192, 0, 0);
            this.turnAllOff.BackColor = Color.DodgerBlue;
            this.turnAllOff.ForeColor = Color.White;
            this.turnAllOff.Height = largeHeight;
            this.turnAllOff.Width = largeWidth;
            this.turnAllOff.Name = "turnAllOff";
            this.turnAllOff.Text = "Turn All Lights Off";
            this.turnAllOff.Location = new Point(largeWidth, smallHeight);
            this.turnAllOff.Font = new Font("Microsoft Sans Serif", 15.75F, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.turnAllOff.Click += new System.EventHandler(this.TurnAllOff_onClick);
            this.Controls.Add(this.turnAllOff);
        }

        private void LEDButtons_onClick(object sender, EventArgs e)
        {
            if (this.columnButton_onClick != null)
            {
                if (((Button)sender).BackgroundImage == onImage)
                {
                    ((Button)sender).BackgroundImage = offImage;
                }
                else if (((Button)sender).BackgroundImage == offImage)
                {
                    ((Button)sender).BackgroundImage = onImage;
                }
                else
                    /*ignore*/;

                this.columnButton_onClick(sender, e);
            }
        }

        private void TurnAllOn_onClick(object sender, EventArgs e)
        {
            foreach (Button button in LEDButtons)
            {
                button.BackgroundImage = offImage;
                button.Enabled = false;
            }

            this.columnButton_onClick(sender, e);
        }

        private void TurnAllOff_onClick(object sender, EventArgs e)
        {
            foreach (Button button in LEDButtons)
            {
                button.BackgroundImage = onImage;
                button.Enabled = true;
            }

            this.columnButton_onClick(sender, e);
        }

        public void DisableButtons()
        {
            this.Enabled = false;
        }

        public void EnableButtons()
        {
            this.Enabled = true;
        }
    }
}
